document.querySelector('.login a').onclick = function () {
  document.querySelector('.login').style.display = 'none'
  document.querySelector('.register').style.display = 'block'
}
document.querySelector('.register a').onclick = function () {
  document.querySelector('.register').style.display = 'none'
  document.querySelector('.login').style.display = 'block'
}
let shit = document.querySelector('.login [name=username]')
let fuck = document.querySelector('.login [name=password]')
let beach = document.querySelector('.register [name=username]')
let money = document.querySelector('.register [name=password]')
abc(shit, '用户名', /^\S{2,15}$/, 2)
abc(fuck, '密码', /^\S{6,15}$/, 6)
abc(beach, '用户名', /^\S{2,15}$/, 2)
abc(money, '密码', /^\S{6,15}$/, 6)

function abc(ab, ac, av, an) {
  ab.oninput = function () {
    if (this.value.trim() == '') {
      this.nextElementSibling.style.display = 'block'
      this.nextElementSibling.innerHTML = ac + '不能为空'
      return
    } else {
      this.nextElementSibling.style.display = 'none'
      this.nextElementSibling.innerHTML = ''
    }
    if (av.test(this.value)) {
      this.nextElementSibling.style.display = 'none'
      this.nextElementSibling.innerHTML = ''
    } else {
      this.nextElementSibling.style.display = 'block'
      this.nextElementSibling.innerHTML = ac + `必须是${an}位`
    }
  }
}
let form1 = document.querySelector('.register form')
form1.onsubmit = function (e) {
  e.preventDefault()
  axios({
    url: '/api/register',
    method: 'post',
    data: $(form1).serialize()
  }).then(({
    data: res
  }) => {
    //成功回调
    /* if (res.code == 1) {
      return toastr.warning(res.message)
    } */
    if (res.code == 0) {
      toastr.success(res.message)
      form1.reset()
      document.querySelector('.register a').click()
    }
  });
}
let form2 = document.querySelector('.login form')
form2.onsubmit = function (e) {
  e.preventDefault()
  axios({
    url: '/api/login',
    method: 'post',
    data: $(form2).serialize()
  }).then(({
    data: res
  }) => {
    if (res.code == 0) {
      toastr.success(res.message)
      location.href = './index.html'
      localStorage.setItem('mytoken', res.token)
    }
  });
}